/*
 * Título: gestHora. Aplicación web de gestión de horarios para conservatorios.
 * Alumnos:
 * - Javier Pardo Muñoz. I.T. Informática de Gestión
 * - Antonio Prior Cazorla. I.T. Informática de Gestión
 * Director: Dr. José Raúl Romero Salguero.
 *     
 *  Copyright (C) 2013 Javier Pardo Muñoz / Antonio Prior Cazorla
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.uco.gesthora.data.gestorPropiedades;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class GestorPropiedades {

    Properties p = new Properties();

    public GestorPropiedades(String nombreFichero) {

        InputStream in;

        try {
            in = new FileInputStream(nombreFichero);
            p.load(in);
            in.close();
        } catch (IOException e) {
        }

    }
    
    public GestorPropiedades(String path, String nombreFichero) {

        InputStream in;

        try {
            in = new FileInputStream(path + "/" + nombreFichero);
            p.load(in);
            in.close();
        } catch (IOException e) {
        }

    }

    public String getPropiedad(String propiedad) {

        return p.getProperty(propiedad);
    }

    public boolean setPropiedad(String propiedad, String valor) {

        try {

            p.setProperty(propiedad, valor);
            return true;

        } catch (Exception e) {
            return false;
        }

    }

    public boolean modPropiedad(String propiedad, String valor) {

        Enumeration enumProp = p.propertyNames();
        String aux;

        while (enumProp.hasMoreElements()) {

            aux = (String) enumProp.nextElement();

            if (aux.equals(propiedad)) {
                p.setProperty(aux, valor);
                return true;
            }
            
        }

        return false;
    }
    
   public void guardarProperties(String nombreFichero, String comentario) {

        OutputStream out;

        try {
            out = new FileOutputStream(nombreFichero);
            p.store(out, comentario);
            out.close();
        } catch (IOException e) {
        }

    }

    public void guardarProperties(String path, String nombreFichero, String comentario) {

        OutputStream out;

        try {
            out = new FileOutputStream(path + "/" + nombreFichero);
            p.store(out, comentario);
            out.close();
        } catch (IOException e) {
        }

    }
}
